package net.ssta.common;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.util.bill.chargeobj.CostNumberConstants;

public class StyleConstants {
	public static String styleOption="";
	public static String stylePath="common";
	private static Logger log = Logger.getLogger(StyleConstants.class.getClass()); 
	

	public static void initStyleOption(){
		Connection conn=null;
		try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql="SELECT * FROM T_COSTNUM";
	    	RecordSet rs=dbe.find(sql);
	    	if(rs!=null&&rs.size()>0){
	    		styleOption=rs.get(0).getString("STYLEOPTION");
	    		stylePath="common"+styleOption;
	    	}else{
	    		styleOption="";
	    		stylePath="common";
	    	}
		} catch (SQLException e) {
			log.error("获取显示风格出错："+e);
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		
	}
	
	
	/**
	 * 切换样式
	 * @param _sytleOption
	 */
	public static void updateStyleOption(String _sytleOption){
		Connection conn=null;
		try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    		conn.setAutoCommit(false);
    		
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql="SELECT * FROM T_COSTNUM";
	    	RecordSet rs=dbe.find(sql);
	    	if(rs==null||rs.size()<1){
	    		Record r=new Record();
	    		r.setConnection(conn);
	    		r.setEntityName("T_COSTNUM");
	    		r.setPrimaryKey("COSTID");
	    		r.addData("STYLEOPTION",_sytleOption);
	    		r.create();
	    	}else{
	    		sql=" update T_COSTNUM set STYLEOPTION='"+_sytleOption+"'" ;
	    		dbe.execute(sql);
	    	}
	    	
	    	
	    	updateCmsChannle(_sytleOption);
	    	

	        	
	    	conn.commit();
	    	initStyleOption();
		} catch (SQLException e) {
			log.error("更新显示风格出错："+e);
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		
	}
	
	
	
	
	/**
	 * 更新cms频道
	 * @param _kjcg
	 * @param _unit
	 * @param _expert
	 */
	public static void updateCmsChannle(String _sytleOption)throws SQLException{
		Connection conn=null;
		try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_CMS);
    		conn.setAutoCommit(false);
    		
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql="UPDATE CMS_CHANNELPOSITION SET   CONTENT=REPLACE(CONTENT, '/achieve"+StyleConstants.styleOption+"/', '/achieve"+_sytleOption+"/')    ";
	    	dbe.execute(sql);
	    	sql="UPDATE CMS_CHANNELPOSITION SET CONTENT=REPLACE(CONTENT, '/common"+StyleConstants.styleOption+"/', '/common"+_sytleOption+"/')";
	    	dbe.execute(sql);
	    	sql="UPDATE CMS_CHANNELPOSITION SET  CONTENT=REPLACE(CONTENT, '/market"+StyleConstants.styleOption+"/', '/market"+_sytleOption+"/')";
	    	dbe.execute(sql);
	    	sql="update CMS_LAYOUT set   URL=REPLACE(URL, '/safetytech"+StyleConstants.styleOption+"/', '/safetytech"+_sytleOption+"/')     WHERE SITE_ID = 3 ";
	    	dbe.execute(sql);
	    	conn.commit();
	    	 
		} catch (SQLException e) {
			 throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		
	}
	
	static{
		initStyleOption();
	}

}
